﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;
using System.Text;

namespace WebSite.Phone
{
    public partial class Detail : System.Web.UI.Page
    {
        private string id = "";
        private Blog binfo = null;
        public string blogtitle = "";
        public string bloguserid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            id = Request.QueryString["id"] + "";
            if (!Page.IsPostBack)
            {
                LoadContent();
                BindOtherBlog();
                BindBlogType();
                BindUserOtherBlog();
            }
        }

        #region 加载内容
        private void LoadContent()
        {
            //Blog binfo = new Blog();
            BlogHelper bhelper = new BlogHelper();
            binfo = bhelper.GetBlog(id);

            litTitle.Text = binfo.Title;
            litBlogPVNum.Text = binfo.PVCount.ToString();
            litCreateDate.Text = binfo.CreateDate.ToString("yyyy-MM-dd HH:mm");
            litContent.Text = binfo.BlogContent.ToString();

            litNickName1.Text = binfo.NickName;
            litNickName2.Text = binfo.NickName;
            litNickName3.Text = binfo.NickName;
            bloguserid = binfo.UsersID;
            blogtitle = binfo.Title;

            DateTime createdate = binfo.CreateDate;
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", bloguserid);
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "BlogBigType", "图文");
            c.Add(CriteriaType.LessThan, "CreateDate", createdate);

            string[] fields = new string[] { "ID", "Title", "HasPic", "Flag", "CreateDate" };

            DataTable dtfront = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 1, fields);

            if (dtfront != null && dtfront.Rows.Count > 0)
            {
                string frontblogid = dtfront.Rows[0]["ID"] + "";
                string title = dtfront.Rows[0]["Title"] + "";
                string haspic = dtfront.Rows[0]["HasPic"] + "";

                string strtitle = "";
                strtitle = Tools.GetFirstString(title, 26);

                if (haspic == "1")
                {
                    strtitle += "(图)"; 
                }
                lblFrontBlog.Text = "前一篇：<a href=\"detail.aspx?id=" + frontblogid + "\">" + strtitle + "</a>";
            }

            Criteria c1 = new Criteria();
            c1.Add(CriteriaType.Equals, "UsersID", bloguserid);
            c1.Add(CriteriaType.Equals, "Flag", 0);
            c1.Add(CriteriaType.Equals, "BlogBigType", "图文");
            c1.Add(CriteriaType.MoreThan, "CreateDate", createdate);

            DataTable dtbehind = bhelper.QueryBlogBySearchToTable(c1, new Order[] { new Order("CreateDate", OrderMode.Asc) }, 0, 1, fields);

            if (dtbehind != null && dtbehind.Rows.Count > 0)
            {
                string behindblogid = dtbehind.Rows[0]["ID"] + "";
                string title = dtbehind.Rows[0]["Title"] + "";
                string haspic = dtbehind.Rows[0]["HasPic"] + "";

                string strtitle = "";
                strtitle = Tools.GetFirstString(title, 26);

                if (haspic == "1")
                {
                    strtitle += "(图)";
                }
                lblBehindBlog.Text = "后一篇：<a href=\"Detail.aspx?id=" + behindblogid + "\">" + strtitle + "</a>";
            } 
        }
        #endregion

        #region 加载同分类其它创作
        private void BindOtherBlog()
        {
            string blogtypeid = binfo.BlogTypeID;
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "BlogTypeID", blogtypeid);
            c.Add(CriteriaType.NotEquals, "ID", id);
            c.Add(CriteriaType.Equals, "Flag", 0);

            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "Flag", "CreateDate" };
            DataTable dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 10, null);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtUserBlog.Columns.Add(dcTitle);

            if (dtUserBlog != null && dtUserBlog.Rows.Count > 0)
            {
                foreach (DataRow dr in dtUserBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 32);

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        strtitle += "(图)";
                    }
                    else
                    {
                    }
                    dr["strTitle"] = strtitle;
                }
            }

            rptOtherBlog.DataSource = dtUserBlog;
            rptOtherBlog.DataBind();
        }
        #endregion

        #region 加载类别
        private void BindBlogType()
        {
            BlogTypeHelper bthelper = new BlogTypeHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "BlogBigType", "图文");
            DataTable dtBlogType = bthelper.QueryBlogTypeBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 10, null);

            rptBlogType1.DataSource = dtBlogType;
            rptBlogType1.DataBind();
        }
        #endregion

        #region 加载作者其它创作
        private void BindUserOtherBlog()
        {
            string blogtypeid = binfo.BlogTypeID;
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", bloguserid);
            c.Add(CriteriaType.Equals, "BlogBigType", "图文");
            c.Add(CriteriaType.NotEquals, "ID", id);
            c.Add(CriteriaType.Equals, "Flag", 0);

            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "Flag", "CreateDate" };
            DataTable dtUserBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 10, null);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtUserBlog.Columns.Add(dcTitle);

            if (dtUserBlog != null && dtUserBlog.Rows.Count > 0)
            {
                foreach (DataRow dr in dtUserBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 32);

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        strtitle += "(图)";
                    }
                    else
                    {
                    }
                    dr["strTitle"] = strtitle;
                }
            }

            rptUserOtherBlog.DataSource = dtUserBlog;
            rptUserOtherBlog.DataBind();
        }
        #endregion
    }
}